package com.chelun.support.clmutiplyfragment;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chelun.support.clmutiplyfragment.model.ListItemBean;
import com.hannesdorfmann.adapterdelegates3.AdapterDelegatesManager;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by leo on 2016/04/19.
 *
 * 多元列表Adapter
 */
public class MultiFunctionListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TYPE_FOOTER = Integer.MIN_VALUE;
    private boolean mHasLoadMore ;
    private List<ListItemBean> mDatas;
    private AdapterDelegatesManager<List<ListItemBean>> mDelegatesManager;
    private ClMultiYFootView mFooterView;

    public MultiFunctionListAdapter(AdapterDelegatesManager<List<ListItemBean>> delegatesManager, ClMultiYFootView footerView) {
        mDelegatesManager = delegatesManager;
        mFooterView = footerView;
        mDatas = new ArrayList<>();
    }

    public void setHasLoadMore(boolean hasLoadMore) {
        this.mHasLoadMore = hasLoadMore;
    }

    public boolean getHasLoadMore() {
        return mHasLoadMore;
    }

    public List<ListItemBean> getItem() {
        return mDatas;
    }

    public void setFootView(ClMultiYFootView footerView) {
        if (mHasLoadMore) {
            return;
        }
        mHasLoadMore = true;
        this.mFooterView = footerView;
    }












    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1 && mHasLoadMore) {
            return TYPE_FOOTER;
        }

        return mDelegatesManager.getItemViewType(mDatas, position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER ) {
            return new FooterHolder(mFooterView);
        }
        return mDelegatesManager.onCreateViewHolder(parent, viewType);
    }

    static class FooterHolder extends RecyclerView.ViewHolder {
        public FooterHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FooterHolder) {
            return ;
        }
        mDelegatesManager.onBindViewHolder(mDatas, position, holder);
    }

    @Override
    public int getItemCount() {
        int count = mDatas.size();
        if (mHasLoadMore) {
            count += 1;
        }
        return count;
    }

    public void setItem(List<ListItemBean> list) {
        mDatas.clear();
        mDatas.addAll(list);
        notifyDataSetChanged();
    }

    public void changeItem(int position, ListItemBean item) {
        mDatas.remove(position);
        mDatas.add(position, item);
        notifyDataSetChanged();
    }

    public void removeRangeItem(int positionStart, int itemCount) {
        for (int i = positionStart; i < positionStart + itemCount; i++) {
            mDatas.remove(positionStart);
        }
        notifyItemRangeRemoved(positionStart, itemCount);
    }

    public void removeItem(int position) {
        mDatas.remove(position);
        notifyItemRemoved(position);
    }

    public void addLoadMoreItem(List<ListItemBean> list) {
        final int startPosition = getItemCount();
        mDatas.addAll(list);
        notifyItemRangeInserted(startPosition, list.size());
    }
}
